// {{变量}}
// {% 语句 %}
// 字符串 => JS 语句执行

/* const str = "console.log('Hello World')"
const temp = new Function(str)
temp() */

/* const str = "console.log('Hello World', a, b)"
// 最后参数是【函数体】的内容，其他参数是形参，实参是调用 temp 时传递的
const temp = new Function('a', 'b', str)
temp(1, 2) */

/* const sName = 'ifer'
const str = "return '<div>{{sName}}</div>'"
const test = new Function('sName', str)
// 函数最终样子
console.log(test.toString())
// 函数返回的样子
console.log(test())

// return '<div>{{sName}}</div>'
// return '<div>' + sName + '</div>'
// return `<div>${sName}</div>` */

/* const fs = require('node:fs')
const str = fs.readFileSync('./temp.ifer', 'utf8')

// 把 {{ sName }} 处理成 `${sName}`
const mustacheReg = /\{\{([^\}]+)\}\}/g
const temp = "return `" + str.replace(mustacheReg, ($0, $1) => "${" + $1.trim() + "}") + "`";
const g = new Function('sName', 'age', 'o', temp)

// console.log(g.toString())

const sName = 'ifer'
const age = 18
const o = { address: '河南' }
// 执行代码
const res = g(sName, age, o)
fs.writeFileSync('./index.html', res) */

/* const fs = require("node:fs");
const str = fs.readFileSync("./temp.ifer", "utf8");

// 把 {{ sName }} 处理成 `${sName}`
const mustacheReg = /\{\{([^\}]+)\}\}/g;
const temp = "with(o){return `" + str.replace(mustacheReg, (_, $1) => "${" + $1.trim() + "}") + "`}";
const g = new Function("o", temp);

const o = { name: 'ifer', age: 18, address: "河南" };
// 执行代码
const res = g(o);
fs.writeFileSync("./index.html", res); */

// {% %}
// {% => `
// %} => str +=`

/* function anomymous(obj) {
  let str = ''
  with(obj) {
    str += ``
    if(bBar) {
      str += `<div>${name}</div>`
    } else {
      str += `<div>無</div>`
    }
    str += ``
  }
  return str
} */

const fs = require("node:fs");
const str = fs.readFileSync("./temp.ifer", "utf8");

// 把 {{ sName }} 处理成 `${sName}`
const mustacheReg = /\{\{([^\}]+)\}\}/g;
let temp = "let str = ''; with(o){ str += `" + str.replace(mustacheReg, (_, $1) => "${" + $1.trim() + "}") + "`} return str";

const reg = /\{\%([^\%]+)\%\}/g
temp = temp.replace(reg, (_, $1) => "`\n" + $1 + "str+=`")

const g = new Function("o", temp);

const o = { name: 'ifer', age: 18, address: "河南", bBar: false, str1: 'a', str2: 'b' };
// 执行代码
// console.log(g.toString())
const res = g(o);
fs.writeFileSync("./index.html", res);

